home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 709 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.5 KB

  1. From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
  2. Message-ID: <4i7p63$4ch@mulga.cs.mu.OZ.AU>
  3. X-Original-Date: 14 Mar 1996 00:27:47 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 14 Mar 96 04:21:15 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: String value of enum
  9. Organization: Comp Sci, University of Melbourne
  10. References: <4i5sf3$89c@hermes.is.co.za> <Do81tp.H9u@rsvl.unisys.com>
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBFAgUBMUeezeEDnX0m9pzZAQHUSwF/VToEYFn/o9hFmMPUE/7EKKlgzd9aJk42
  13.     yKnKf2fDyk3U1AE7AJaQXGq+Mfrkz3Zf
  14.     =CBkz
  15.  
  16. mtm4@rsvl.unisys.com (Michael McCormick) writes:
  17.  
  18. >"W. Dicks" <wd@isis.co.za> shared the following on 13 Mar 96 07:31:13 GMT:
  19. >
  20. >>The system that I'm working often needs to know the string 
  21. >>value of an enum. [...] Is it not possible that such 
  22. >>a functionality can be built into enums?
  23. >
  24. >What you are essentially asking for is language support for converting
  25. >a label into a string respresentation of its name.  That is a very
  26. >unusual feature to find in any language, since it would require the
  27. >compiler to place the symbolic dictionary in the executable file. 
  28. >I don't think there's a snowball's chance of this getting into C++.
  29.  
  30. I wouldn't call it "very unusual" -- there are many existing languages
  31. which have similar features, including Haskell, Prolog, and Java -- not
  32. to mention C++ itself!  If `x' is a polymorphic class variable, then
  33. the C++ expression `typeid(x).name()' returns a string representation
  34. of the class name.
  35.  
  36. >Since enum is by definition an integral type, why not assign values to your
  37. >enumerators that can be used as indexes into a string?:
  38. >
  39. > enum week {MON=0,TUE=4,WED=8,THU=12,FRI=16,SAT=20,SUN=24};
  40. > const char * days = "MON\0TUE\0WED\0THU\0FRI\0SAT\0SUN";
  41. >
  42. > char const * weekImage(week day) = days[day];
  43.  
  44. I think a much cleaner workaround would be to use an array of strings:
  45.  
  46. enum week {MON, TUE, WED, THU, FRI, SAT, SUN};
  47. const char * days[] = { "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" };
  48. char const * weekImage(week day) { return days[day]; }
  49.  
  50. --
  51. Fergus Henderson                 WWW: http://www.cs.mu.oz.au/~fjh
  52. fjh@cs.mu.oz.au                  PGP: finger fjh@128.250.37.3
  53. ---
  54. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  55. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  56. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  57. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  58. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  59.